package meijuli.com.join51.business.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import meijuli.com.join51.business.R;
import meijuli.com.join51.business.model.WithdrawRule;
import java.util.List;

/**
 * 兑换规则适配器
 *
 * @author Wizong 2017-05-19 13:09
 */
public class WithdrawWayAdapter extends RecyclerView.Adapter<WithdrawWayAdapter.VH> {

    private LayoutInflater mInflater;
    private List<WithdrawRule> mWithdrawRules;
    private OnCheckClick mCheckListener;

    private int mSelectedPosition = -1;

    private CheckBox lastCheckWithdrawWay;

    public WithdrawWayAdapter(Context context, List<WithdrawRule> withdrawRules) {
        this.mWithdrawRules = withdrawRules;
        this.mInflater = LayoutInflater.from(context);
    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        return new VH(mInflater.inflate(R.layout.item_withdraw_way, null));
    }
    @Override
    public void onBindViewHolder(VH holder, int position) {
        WithdrawRule withdrawRule = mWithdrawRules.get(position);
        holder.textName.setText(withdrawRule.getRuleName());
        holder.textDesc.setText(withdrawRule.getDescribe());

        if (mSelectedPosition == position) {
            if (lastCheckWithdrawWay != null) {
                lastCheckWithdrawWay.setChecked(false);
            }
            holder.checkWithdrawWay.setChecked(true);
            lastCheckWithdrawWay = holder.checkWithdrawWay;
        } else {
            holder.checkWithdrawWay.setChecked(false);
        }
    }

    @Override
    public int getItemCount() {
        return mWithdrawRules.size() > 0 ? mWithdrawRules.size() : 0;
    }

    public void setWithdrawRules(List<WithdrawRule> mWithdrawRules) {
        this.mWithdrawRules = mWithdrawRules;
    }

    public class VH extends RecyclerView.ViewHolder {
        TextView textName;
        TextView textDesc;
        CheckBox checkWithdrawWay;

        public VH(View itemView) {
            super(itemView);
            textName = itemView.findViewById(R.id.text_withdraw_way_name);
            textDesc = itemView.findViewById(R.id.text_withdraw_way_desc);
            checkWithdrawWay = itemView.findViewById(R.id.check_withdraw_way);

            checkWithdrawWay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mSelectedPosition = getAdapterPosition();
                    if (mSelectedPosition > -1) {
                        notifyItemChanged(mSelectedPosition);
                    }
                    if (mCheckListener != null) {
                        mCheckListener.checkBoxClick(mSelectedPosition);
                    }
                }
            });
        }
    }

    public interface OnCheckClick {
        void checkBoxClick(int position);
    }

    public void setOnCheckClickListener(OnCheckClick listener) {
        this.mCheckListener = listener;
    }
}
